/**
 * 
 */
package com.seguriapp.controllers;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.database.Cursor;
import android.util.Log;

import com.seguriapp.communication.WebServiceConsumer;
import com.seguriapp.dao.CrimeCategoryDao;
import com.seguriapp.entities.CrimeCategoryEntity;
import com.seguriapp.entities.CrimeEntity;
import com.seguriapp.util.Constants;

/**
 * @author pjohnson
 * 
 */
public class CrimeController {

	private Context mContext;

	public CrimeController(final Context context) {
		mContext = context;
	}

	/**
	 * GetCrimeList from the server
	 * 
	 * @param mapOptions
	 * @param zoom
	 * @return
	 */
	public List<CrimeEntity> getCrimeList(final String mapOptions) {
		final WebServiceConsumer webConsumer = WebServiceConsumer
				.getInstance(mContext);
		final JSONObject postObject = new JSONObject();
		ArrayList<CrimeEntity> lstCrime = new ArrayList<CrimeEntity>();
		try {
			postObject.put("mapOptions", mapOptions);
			// postObject.put("zoomLevel", 5);
			// postObject.put("deviceType",
			// com.seguriapp.util.Constants.DEVICE_TYPE);
			final String strCrimeActivity = webConsumer
					.getCrimeMapActivity(postObject);
			final JSONObject jSonCrime = new JSONObject(strCrimeActivity);
			final JSONArray jSonComplaintsLst = jSonCrime
					.getJSONArray("complaints");

			for (int i = 0; i < jSonComplaintsLst.length(); i++) {
				final JSONObject crimeObject = jSonComplaintsLst
						.getJSONObject(i);

				final CrimeEntity crime = new CrimeEntity();
				crime.setLatitude(crimeObject.getDouble("crimelatitude"));
				crime.setLongitude(crimeObject.getDouble("crimelongitude"));

				final JSONObject jSonCategory = crimeObject
						.getJSONObject(Constants.CATEGORY_TAG);
				final CrimeCategoryEntity crimeCategory = new CrimeCategoryEntity();
				crimeCategory.setIdCategory(jSonCategory
						.optInt(Constants.ID_TAG));
				crimeCategory.setDescription(jSonCategory
						.optString(Constants.DESCRIPTION_TAG));
				crimeCategory.setName(jSonCategory
						.optString(Constants.NAME_TAG));

				crime.setCategory(crimeCategory);

				lstCrime.add(crime);
			}
		} catch (JSONException e) {
			lstCrime = null;
			Log.e("Error Json", e.getMessage());
		}

		return lstCrime;
	}

	/**
	 * Devuelve un string con con los ids de las categorias seleccionadas
	 * 
	 * @return
	 */
	public String getSelectedCategories(final boolean isCrime) {
		final Cursor cursor = mContext.getContentResolver().query(
				CrimeCategoryDao.CONTENT_URI, null,
				CrimeCategoryDao.COL_SELECTED + " = ? ", new String[] { "1" },
				null);
		final StringBuffer rpta = new StringBuffer();
		while (!cursor.isAfterLast()) {
			rpta.append(cursor.getString(cursor
					.getColumnIndexOrThrow(CrimeCategoryDao.COL_CID)));
			rpta.append(",");
			cursor.moveToNext();
		}
		return rpta.toString();
	}
}
